/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
 ***/
package stripesbook.action.user;

import java.util.Collection;
import javax.annotation.security.PermitAll;
import net.sourceforge.stripes.action.HttpCache;
import net.sourceforge.stripes.action.Resolution;
import stripesbook.action.BaseActionBean;
import stripesbook.model.Contact;
import stripesbook.model.User;
import stripesbook.view.ContactTable;
import stripesbook.view.Menu;

@HttpCache(allow = false)
@PermitAll
public abstract class ContactBaseActionBean extends BaseActionBean {
  @Override
  public Menu.Section getCurrentSection() {
    return Menu.Section.ContactList;
  }

  private Contact contact;

  public Contact getContact() {
    return contact;
  }

  public void setContact(Contact contact) {
    User user = contact.getUser();
    if (user == null || getUser().equals(user)) {
      this.contact = contact;
    }
  }

  public Resolution streamContactTable() {
    Collection<Contact> contacts = getUser().getContacts();
    return stream(tag(ContactTable.class).init(contacts));
  }
}
